package auth

import (
	"github.com/gin-gonic/gin"
	v1 "last/app/controllers/v1"
	adminModel "last/app/models/admin"
	"last/app/requests"
	adminRequest "last/app/requests/admin"
	"last/pkg/respose"
	"strconv"
)

type FocusController struct {
	v1.BaseController
}

// Store 新增
func (f *FocusController) Store(c *gin.Context) {
	var params = adminRequest.FocusParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var focusInfo = &adminModel.Focus{
		Title: params.Title,
		Img:   params.Img,
	}
	resultBool := focusInfo.Store(focusInfo)
	if !resultBool {
		respose.Failed(c, "新增失败")
		return
	}
	respose.Success(c, "", "新增成功")
}

// List 列表
func (f *FocusController) List(c *gin.Context) {
	var focusInfo = &adminModel.Focus{}
	page, _ := strconv.Atoi(c.Query("page"))
	pageSize, _ := strconv.Atoi(c.Query("page_size"))
	where := make(map[string]interface{}, 1)
	where["title"] = c.DefaultQuery("title", "")

	err, total, lists := focusInfo.List(page, pageSize, where)

	if err != nil {
		respose.Failed(c, err.Error())
		return
	}

	result := make(map[string]interface{}, 1)
	result["total"] = total
	result["list"] = lists
	respose.Success(c, result, "获取成功")
}

// Show 详情
func (f *FocusController) Show(c *gin.Context) {
	id := c.Param("id")
	var focusInfo = &adminModel.Focus{}
	err := focusInfo.Show(id, focusInfo)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, focusInfo, "获取成功")
}

// Edit 更新
func (f *FocusController) Edit(c *gin.Context) {
	var params = adminRequest.FocusParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var focusInfo = &adminModel.Focus{}
	editData := make(map[string]interface{}, 1)
	editData["title"] = params.Title
	editData["img"] = params.Img
	result := focusInfo.Edit(params.Id, editData)
	if result != nil {
		respose.Failed(c, "更新失败")
		return
	}
	respose.Success(c, "", "更新成功")
}

// Remove 删除
func (f *FocusController) Remove(c *gin.Context) {
	id := c.Param("id")
	var focusInfo = &adminModel.Focus{}
	err := focusInfo.Remove(id, focusInfo)
	if err != nil {
		respose.Failed(c, "删除失败")
		return
	}
	respose.Success(c, "", "删除成功")
}
